﻿using Novel.Web.Models.Novel.output;
using System.Text.RegularExpressions;
using XjjXmm.FrameWork.ToolKit;
using XjjXmm.Infrastructure.Repository;
using XjjXmm.Spider.Entity;

namespace Novel.Web.Models.Novel
{
    public class NovelFacade
    {
        private readonly NovelService _novelService;
        private readonly MenuService _menuService;

        public NovelFacade(NovelService novelService, MenuService menuService)
        {
            this._novelService = novelService;
            this._menuService = menuService;
        }

        public async  Task<dynamic> FetchHome(int page)
        {
            Dictionary<string, object> data = new Dictionary<string, object>();

            var menus = _menuService.GetMenus();
            data.Add("Menus", menus);

           

            data.Add("Tops", await _novelService.GetTop(null, 4));
            data.Add("Hots", await _novelService.GetHot(null, 32));

           
            foreach (var menuEnum in  EnumKit.GetEnums<MenuEnum>()) {
                data.Add(menuEnum.ToString(), await GetNovels(menuEnum));
               
            }

         
            var pageInput = new PageInput<string>();
            pageInput.CurrentPage = page;
            pageInput.PageSize = 30;
            data.Add("Lastest",await _novelService.GetNovelByCategory(pageInput));

            return data;
        }

        public async Task<dynamic> FetchSub(string category, int page)
        {
            var currentMenu = EnumKit.GetEnums<MenuEnum>().FirstOrDefault(t => t.ToString().ToLower() == category.ToLower());

            Dictionary<string, object> data = new Dictionary<string, object>();

            var menus = _menuService.GetMenus();
            data.Add("Menus", menus);

            //var currentMenu = MenuEnum.XuanHuan;
            data.Add("Xiaoshuo", currentMenu.ToDescription());


            data.Add("Tops", await _novelService.GetTop(currentMenu, 6));
            data.Add("Hots", await _novelService.GetHot(currentMenu, 32));

            var pageInput = new PageInput<string>();
            pageInput.CurrentPage = page;
            pageInput.PageSize = 30;
            pageInput.Filter = currentMenu.ToDescription();
            data.Add("Lastest", await _novelService.GetNovelByCategory(pageInput));

            return data;
        }

        public async Task<dynamic> FetchChapters(long novelId)
        {
            Dictionary<string, object> data = new Dictionary<string, object>();

            var menus = _menuService.GetMenus();
            data.Add("Menus", menus);

            data.Add("Novel", await _novelService.GetNovel(novelId));
            data.Add("Chapters", await _novelService.GetChaptersByNovelId(novelId));

            data.Add("Views", await _novelService.GetViews(novelId));

            return data;
        }


        public async Task<dynamic> FetchContent(long nid, long cid)
        {
            Dictionary<string, object> data = new Dictionary<string, object>();

            var menus = _menuService.GetMenus();
            data.Add("Menus", menus);

            var novel = await _novelService.GetNovel(nid);
            data.Add("Novel", novel);

            var chapters = await _novelService.GetChaptersByNovelId(novel.Id);
            var index = chapters.FindIndex(x => x.Id == cid);
            var current = chapters[index];

            //Regex reg = new Regex("(&nbsp;.*?\r)");
            //var groups = reg.Replace(current.Content, "<p>$1</p>");
            current.Contents = await _novelService.GetChapterContent(nid,cid);
           // current.Content = Regex.Replace(current.Content, "(&nbsp;.*?\r\n)", "<p>$1</p>");
            //current.Content = Regex.Replace(current.Content, "(&nbsp;.*?\n)", "<p>$1</p>");

            data.Add("Chapter", current);
            if (index > 0)
            {
                data.Add("PrevChapter", chapters[index - 1]);
            }
            else
            {
                data.Add("PrevChapter", null);
            }

            if (index < chapters.Count - 1)
            {
                data.Add("NextChapter", chapters[index + 1]);
            }
            else
            {
                data.Add("NextChapter", null);
            }


            data.Add("Views", await _novelService.AddViews(nid));

            return data;
        }

        private async Task<IndexNovelOutput> GetNovels(MenuEnum menu)
        {
            var pageInput = new PageInput<string>
            {
                CurrentPage = 1,
                PageSize = 13,
                Filter = menu.ToDescription()
            };
            var items = (await _novelService.GetNovelByCategory(pageInput)).Data;

            return new IndexNovelOutput
            {
                Title = menu.ToDescription(),
                Top = items.FirstOrDefault(),
                Items = items.Skip(1)?.ToList()
            };
        }
    }
}
